package com.denlaku.longan.handlers;

import com.denlaku.longan.event.MessageEvent;
import com.denlaku.longan.event.MessageEventHandler;
import com.denlaku.longan.service.DataSetService;
import com.denlaku.longan.service.DataOriginService;
import com.denlaku.longan.vo.ObjectTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author tianx
 */
@Component
public class DataObjectTagEventHandler implements MessageEventHandler {
    @Autowired
    private DataSetService dataSetService;

    @Autowired
    private DataOriginService dataOriginService;

    @Override
    public Class<?> supportedType() {
        return ObjectTag.class;
    }

    @Override
    public void onEvent(MessageEvent event, long sequence, boolean endOfBatch) throws Exception {
        Object content = event.getContent();
        if (content instanceof ObjectTag) {
            ObjectTag tag = (ObjectTag) content;
            dataSetService.clearTag(tag.getTenantId(), tag.getId());
            dataOriginService.clearTag(tag.getTenantId(), tag.getId());
        }
    }
}
